//
//  CYMyLoanAppliedInfoVC.m
//  CreditAddressBookEE
//
//  Created by LE on 16/2/25.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYMyLoanAppliedInfoVC.h"
#import "CYDiscoveryHttpTool.h"
#import "CYSimpleLoanM.h"
#import "CYCompanyUserTool.h"
#import "CYCarRewardApplyM.h"
#import "CYCreditTool.h"
#import "MBProgressHUD+LE.h"

@interface CYMyLoanAppliedInfoVC ()

@property (weak, nonatomic) IBOutlet UILabel *titleL;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *phone;
@property (weak, nonatomic) IBOutlet UILabel *descriptions;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UILabel *money;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property (weak, nonatomic) IBOutlet UIButton *confrimBtn;

@property(nonatomic, strong) CYCarRewardApplyM *carRewardApply;
@end

@implementation CYMyLoanAppliedInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.bgView.layer.borderWidth = 1;
//    self.bgView.layer.borderColor =CYNaviThemeColor.CGColor;
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.cancelBtn.layer.cornerRadius = 3;
    self.cancelBtn.layer.masksToBounds = YES;
    self.confrimBtn.layer.cornerRadius = 3;
    self.confrimBtn.layer.masksToBounds = YES;
    [self loadData];
}

- (void)loadData {
    [MBProgressHUD showMessage:nil toView:self.view];
    [CYDiscoveryHttpTool handleRewardWithParam:@{@"loanRecordId": self.model.loanRecordId} success:^(CYCarRewardApplyM *carRewardApply) {
        [MBProgressHUD hideHUDForView:self.view];
        self.carRewardApply = carRewardApply;
        [self setupData:carRewardApply];
    } failure:^(NSString *message, BOOL isLogin) {
        [MBProgressHUD hideHUDForView:self.view animated:NO];
        [MBProgressHUD showError:message];
        if (!isLogin) {
            [self.navigationController popToRootViewControllerAnimated:NO];
            [CYCompanyUserTool logoutWithController:self];
        }
    }];
}

- (void)setupData:(CYCarRewardApplyM *)model {
    self.titleL.text = [NSString stringWithFormat:@"您发布的车辆 %@，已被申请悬赏：", model.carNumber];
    self.name.text = model.linkName;
    self.phone.text = model.linkPhone;
    self.descriptions.text = model.memo;
    self.time.text = [CYCreditTool timewithLongLongValue:model.createTime.longLongValue formatter:@"yyyy-MM-dd"];
    self.money.text = [NSString stringWithFormat:@"%@元", self.model.rewardMoney];
}
/*!
 *  @brief  支付悬赏
 */
- (void)payApply {
    if (self.carRewardApply) {
        [MBProgressHUD showMessage:nil toView:self.view];
        [CYDiscoveryHttpTool payApplyWithParam:@{@"applyId": self.carRewardApply.applyId} success:^(NSString *message) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            [MBProgressHUD showSuccess:message];
        } failure:^(NSString *message, BOOL isLogin) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            [MBProgressHUD showError:message];
            if (!isLogin) {
                [self.navigationController popToRootViewControllerAnimated:NO];
                [CYCompanyUserTool logoutWithController:self];
            }
        }];
    } else {
        [MBProgressHUD showError:@"请刷新后重试"];
    }
}
/*!
 *  @brief  取消支付悬赏
 */
- (void)cancelPay {
    if (self.carRewardApply) {
        [MBProgressHUD showMessage:nil toView:self.view];
        [CYDiscoveryHttpTool cancelPayWithParam:@{@"applyId": self.carRewardApply.applyId} success:^(NSString *message) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            [MBProgressHUD showSuccess:message];
        } failure:^(NSString *message, BOOL isLogin) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            [MBProgressHUD showError:message];
            if (!isLogin) {
                [self.navigationController popToRootViewControllerAnimated:NO];
                [CYCompanyUserTool logoutWithController:self];
            }
        }];
    } else {
        [MBProgressHUD showError:@"请刷新后重试"];
    }
}

- (IBAction)confrimBtn:(UIButton *)sender {
    [self payApply];
}
- (IBAction)cancelBtn:(UIButton *)sender {
    [self cancelPay];
}
@end
